package cc.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentFactory;
import androidx.fragment.app.FragmentTransaction;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.SparseArray;

import com.example.music_api.MusicComponentApi;
import com.example.news_api.NewsComponentApi;
import com.google.android.material.tabs.TabLayout;
import com.vanniktech.rxpermission.Permission;
import com.vanniktech.rxpermission.RealRxPermission;

import cc.common.ComponentApi;
import cc.common.manager.ComponentManager;

/**
 * 主界面。
 */
public class MainActivity extends AppCompatActivity {

    private SparseArray<Fragment> fragmentMap = new SparseArray<>();
    private Fragment curFragment;

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RealRxPermission.getInstance(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(permission -> {
            if (permission.state() == Permission.State.GRANTED) {
                onInit();
            }
        });
    }

    void onInit() {
        setContentView(R.layout.activity_main);
        ((TabLayout) findViewById(R.id.tabLayout)).setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                Fragment fragment = fragmentMap.get(tab.getPosition());
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                boolean isAdded = false;
                ComponentApi componentApi = null;
                if (fragment == null) {
                    isAdded = true;
                    if (tab.getPosition() == 0) {
                        componentApi = (MusicComponentApi) ComponentManager.getInstance().get(MusicComponentApi.class);
                    } else {
                        componentApi = (NewsComponentApi) ComponentManager.getInstance().get(NewsComponentApi.class);
                    }
                    if (componentApi != null) {
                        Class clz = componentApi.getMainFragment();
                        if (tab.getPosition() == 2) {
                            clz = ((NewsComponentApi) componentApi).getNewsVMFragment();
                        }
                        fragment = new FragmentFactory().instantiate(getClassLoader(), clz.getName());
                        transaction.add(R.id.flMain, fragment);
                    }
                    fragmentMap.put(tab.getPosition(), fragment);
                }
                if (curFragment != null) {
                    transaction.hide(curFragment);
                }
                if (!isAdded) {
                    transaction.show(fragment);
                }
                transaction.commitAllowingStateLoss();
                curFragment = fragment;
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

}
